package cn.devil.two.utils

import com.typesafe.config.{Config, ConfigFactory}
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

object SCUtiles {
  def createSparkContextInstance(appName: String,
                                 conf: Map[String, String] = Map.empty[String, String]) = {

    val sparkConf = new SparkConf()
      .setAppName(appName)
    if (!clusterSwitch) sparkConf.setMaster("local[*]")
    if (!conf.isEmpty) {
      conf.foreach {
        case (key, value) => sparkConf.set(key, value)
      }
    }

    val sc = new SparkContext(sparkConf)
    sc
  }

  def createSQLContextInstance(sc: SparkContext) = new SQLContext(sc)
      private lazy val application: Config = ConfigFactory.load()

      def clusterSwitch = application.getBoolean("leopard.cluster.switch")
}
